(function ($) {

    /*css样式*/
    var dropCss ='';

    /*引入字体图标css文件*/
    document.write("<link rel='stylesheet' href='//at.alicdn.com/t/font_1017713_tzxgpasx3m.css'>");

    /*引入css样式*/
    document.write("<style type='text/css'>"+dropCss+"</style>");
    var Beautifier = function (ele,opt) {
        this.$element = ele,
            this.default = {
                type:'Android',/*下拉类型 Android和IOS*/
                classify:'only',/*下拉分类 ganged和only（联动下拉和非联动下拉）*/
                data:'',/*只选择日期（data）、选择日期和时间、只选择年、只选择年月、只选择时间*/
                search:false,/*是否开启搜索 false和true */
                more:false,/*是否开启多选 false和true */
                incident:'click',/*事件，click、mouseover、mouseout （点击、鼠标上移、鼠标下移）*/
                name:'',/*name值*/
                val:'',/*当前值*/
            },
            this.options = $.extend({},this.default,opt)
    };
    Beautifier.prototype = {
        beautify:function () {
            /*实例化option*/
            var optionArr = new Array();
            var sele = this.$element.children("option");
            for (var i=0; i<sele.length;i++){
                var checked;
                var optionId = sele[i].value?sele[i].value:'0';
                if(sele[i].selected){
                    checked = "active";
                }else {
                    checked = "";
                }
                optionArr.push([sele[i].label,optionId,checked]);
            }

            /*初始化日历*/

            var now = new Date();
            /*获取当前年*/
            var y = now.getFullYear();
            /*获取当前月*/
            var m = (now.getMonth()+1)<10?("0"+(now.getMonth()+1)):(now.getMonth()+1);
            /*获取当前天*/
            var d = now.getDate()<10?("0"+now.getDate()):now.getDate();
            /*获取当前小时数*/
            var hr = now.getHours()<10?('0'+now.getHours()):now.getHours();
            /*获取当前分钟数*/
            var min = now.getMinutes()<10?('0'+now.getMinutes()):now.getMinutes();
            /*获取当前秒数*/
            var s = now.getSeconds()<10?("0"+now.getSeconds()):now.getSeconds();
            /*获取当前星期*/
            var w = now.getDay();
            

            /*初始化内容*/
            var text ='';

                text += '<div class="poet-select">';
                text += '<div class="option-val">';
                text += '<input type="'+this.options.name+'"  class="drop-input drop_'+this.options.name+'" placeholder="'+optionArr[0][0]+'"/>';
                text += '<span class="drop-html"></span>';
                text += '</div>';
                if(!this.options.data){
                    text += '<ul class="drop_ul drop_ul_'+this.options.name+'">';
                    for(var key in optionArr){
                        text += "<li data-id='"+optionArr[key][1]+"' class='drop_li "+optionArr[key][2]+"'>"+optionArr[key][0]+"</li>";
                    }
                    text += '</ul>';
                }else {
                    text += '';
                }
                text += '</div>';


            $(this.$element).before(text);
            console.log(optionArr);
           $(".drop_li").on('click',function () {
               if($(this).attr("data-id") == '0'){
                   $(this).parents(".poet-select").find(".drop-html").css("opacity",'0');
                   $(this).parents(".poet-select").find(".drop-input").val('');
               }else {
                   $(this).parents(".poet-select").find(".drop-html").css("opacity",'1');
                   $(this).parents(".poet-select").find(".drop-input").val($(this).attr("data-id"));
                   $(this).parents(".poet-select").find(".drop-html").html($(this).html());
               }
               $(this).parents(".poet-label").find("option").attr("selected",false);
               $(this).parents(".poet-label").find("option").eq($(this).index()).attr("selected",true);
               $(this).parents(".poet-select").find(".drop_ul").hide();
               $(this).parents(".poet-select").find(".drop_li").removeClass("active");
               $(this).addClass("active");
           });
            $(".option-val").on(this.options.incident,function () {
                $(".drop_ul").hide();
                $(this).parents(".poet-select").find(".drop_ul").show();

            });
            $(document).on('click',function (e) {
                if ($(e.target).parents(".poet-select").length < 1) {
                    $(".drop_ul").hide();
                }
            });
            var elemen =  this.$element;
            return elemen;
        }
    };
    $.fn.dropped = function(options) {
        //创建Beautifier的实体
        var beautifier = new Beautifier(this, options);
        //调用其方法
        return beautifier.beautify();
    }
})(jQuery);
